<?php
require 'model-db-connection/config.php';
$config = require 'model-db-connection/qrc_conf.properties.php';
?>
<div class="innerLR" style="width: 1000px">
    <a href="index-mainpage.php">Main Page</a> -> <span id="link_to_teamBuilder"></span><span id="link_to_project_order"></span><br/>
</div>
<div class="innerLR" style="width:1000px">
    <div class="widget">
        <div class="widget-head">
            <h4 class="heading">Search Team/Members (ค้นหาทีม/ลูกทีม)</h4>
            <div class="clearfix"></div>
        </div>
        <div class="widget-body innerAll inner-2x">
            <fieldset>
                <legend>Search Type (ประเภทการค้นหา)</legend>
                <table width="100%">
                    <tr>
                        <td><select id="search_builder_change" class="form-control" name="search_builder_change">
                                <option value="0"></option>
                                <option value="1">Search Team Builder(s) (ค้นหา ทีม)</option>
                                <option value="2">Search Team Member(s) (ค้นหา ลูกทีม)</option>
                            </select></td>
                    </tr>
                </table>
            </fieldset>

            <br/>
            <div id="search_team">
                <table width="100%">
                    <tr>
                        <td align="right" style="width:250px">Team Code (หมายเลขทีมช่าง):</td>
                        <td align="left" style="width:250px"><input type="text" class="form-control" id="team_code_search"></td>
                        <td align="right" style="width:250px">Team Name (ชื่อทีม):</td>
                        <td align="left" style="width:250px"><input type="text" class="form-control" id="team_name_search"></td>
                    </tr>
                    <tr>
                        <td align="right" style="width:250px">Team Leader (หัวหน้าทีม):</td>
                        <td align="left" style="width:250px">
                            <select class="form-control" id="team_lead_search" name="team_lead_search">
                                <option value=""></option>
                                <?php
                                $sqlSelectMemType = "SELECT * FROM QRC_MEMBERS WHERE memRole ='60004';";
                                $resultSets = mysql_query($sqlSelectMemType);
                                while ($row = mysql_fetch_array($resultSets)) {
                                    echo '<option value="' . $row['memID'] . '">' . $row['memName'] . '</option>';
                                }
                                ?>
                            </select>
                        </td>
                        <td align="right" style="width:250px"></td>
                        <td align="left" style="width:250px"></td>
                    </tr>
                    <tr>
                        <td align="right" style="width:250px">Type of Service (ประเภทของบริการ):</td>
                        <td align="left" style="width:250px" colspan="3">
                            <select multiple="multiple" style="width: 100%;" id="select2_2" name="type_of_service">
                                <optgroup label="Type of Service">
                                    <?php
                                    $sqlSelectProjectType = "SELECT * FROM QRC_TYPE_OF_SERVICE;";
                                    $resultSet = mysql_query($sqlSelectProjectType);
                                    while ($row = mysql_fetch_array($resultSet)) {
                                        echo '<option value="' . $row['service_id'] . '">' . $row['service_name'] . '</option>';
                                    }
                                    ?> 
                                </optgroup>                              
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width:250px">Team Type (ประเภททีม):</td>
                        <td align="left" style="width:250px">
                            <select class="form-control" id="team_type_search" name="team_type_search">
                                <option value=""></option>
                                <option value="M">M (Main team)</option>
                                <option value="S">S (Sub team)</option>
                                <option value="T">T (Temporary)</option>
                            </select>
                        </td>
                        <td align="right" style="width:250px">Team Manager (ผู้จัดการทีม):</td>
                        <td align="left" style="width:250px">
                            <select class="form-control" id="team_t_manager_search" name="team_t_manager_search">
                                <option value=""></option>
                                <?php
                                $sqlSelectMemType = "SELECT * FROM QRC_MEMBERS WHERE memRole ='60003';";
                                $resultSets = mysql_query($sqlSelectMemType);
                                while ($row = mysql_fetch_array($resultSets)) {
                                    echo '<option value="' . $row['memID'] . '">' . $row['memName'] . '</option>';
                                }
                                ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td style="width:250px;" colspan="4" align="center"><span style="color: red;">*ถ้าต้องการค้นหาทั้งหมด กรุณาเว้นช่องว่าง</span></td>
                    </tr>
                    <tr>
                        <td align="right" style="width:250px" colspan="2"><button class = "btn btn-primary" style="margin-left: 5px" id="team_builder_search_btn">Search (ค้นหา)</button></td>
                        <td align="left" style="width:250px" colspan="2"><button class = "btn btn-primary" style="margin-left: 5px" id="team_builder_reset_btn">Reset (รีเซต)</button></td>

                    </tr>
                </table>
            </div>


            <div id="search_members">
                <table width="100%">
                    <tr>
                        <td align="right" style="width:250px">Member ID (หมายเลขประจำตัว):</td>
                        <td align="left" style="width:250px"><input type="text" class="form-control" id="member_id"></td>
                        <td align="right" style="width:250px">Member Name (ชื่อ-สกุล):</td>
                        <td align="left" style="width:250px"><input type="text" class="form-control" id="member_name"></td>
                    </tr>
                    <tr>
                        <td align="right" style="width:250px">Role (ตำแหน่ง):</td>
                        <td align="left" style="width:250px">
                            <select class="form-control" id="role" name="role">
                                <option value=""></option>
                                <?php
                                $sqlSelectProjectType = "SELECT * FROM QRC_MEMBER_ROLE;";
                                $resultSet = mysql_query($sqlSelectProjectType);
                                while ($row = mysql_fetch_array($resultSet)) {
                                    echo '<option value="' . $row['role_id'] . '">' . $row['role_name'] . '</option>';
                                }
                                ?>
                            </select>
                        </td>
                        <td align="right" style="width:250px">Skill (ความสามารถ):</td>
                        <td align="left" style="width:250px">
                            <select multiple="multiple" style="width: 100%;" id="select3_2" name="skill">
                                <optgroup label="Type of Service">
                                    <?php
                                    $sqlSelectProjectType = "SELECT * FROM QRC_TYPE_OF_SERVICE;";
                                    $resultSet = mysql_query($sqlSelectProjectType);
                                    while ($row = mysql_fetch_array($resultSet)) {
                                        echo '<option value="' . $row['service_id'] . '">' . $row['service_name'] . '</option>';
                                    }
                                    ?> 
                                </optgroup>                              
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width:250px">Team Code (หมายเลขทีมช่าง):</td>
                        <td align="left" style="width:250px">
                            <select class="form-control" id="team_code_in_member_form" name="team_code_in_member_form">
                                <option value=""></option>
                                <?php
                                $sqlSelectProjectType = "SELECT * FROM QRC_TEAM_BUILDER;";
                                $resultSet = mysql_query($sqlSelectProjectType);
                                while ($row = mysql_fetch_array($resultSet)) {
                                    echo '<option value="' . $row['tCode'] . '">' . $row['tCode'] . '</option>';
                                }
                                ?>
                            </select>
                        </td>
                        <td align="right" style="width:250px">Team Name (ชื่อทีม):</td>
                        <td align="left" style="width:250px"><input type="text" class="form-control" id="t_name_in_search"></td>
                    </tr>
                    <tr>
                        <td style="width:250px;" colspan="4" align="center"><span style="color: red;">*ถ้าต้องการค้นหาทั้งหมด กรุณาเว้นช่องว่าง</span></td>
                    </tr>
                    <tr>
                        <td align="right" style="width:250px" colspan="2"><button class = "btn btn-primary" style="margin-left: 5px" id="member_builder_search_btn">Search (ค้นหา)</button></td>
                        <td align="left" style="width:250px" colspan="2"><button class = "btn btn-primary" style="margin-left: 5px" id="member_builder_reset_btn">Reset (รีเซต)</button></td>

                    </tr>
                </table>
            </div>

        </div>
    </div>
</div>
<div class="loader"></div>
<div class="innerLR" style="width:1200px">

    <div id="load_team"></div>
    <div id="load_member"></div>

</div>
<script src="assets/components/plugins/preload/pace/pace.min.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/plugins/preload/pace/preload.pace.init.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/forms/elements/multiselect/assets/lib/js/jquery.multi-select.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/forms/elements/multiselect/assets/custom/js/multiselect.init.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/forms/elements/select2/assets/lib/js/select2.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/forms/elements/select2/assets/custom/js/select2.init.js?v=v1.0.2&sv=v0.0.1"></script>


<script type="text/javascript">
    $(window).load(function() {
        $(".loader").fadeOut("slow");
    });
    var createOrEditState = "Create";
    $(document).ready(function() {

        $("#search_team, #search_members").hide();
        $('#search_builder_change').change(function() {
            var value = $(this).val();
            if (value == 0) {
                $("#search_team, #search_members").hide();
            }
            if (value == 1) {
                $("#search_team").show();
                $("#search_members").hide();
                $("#load_member").empty();
            }
            if (value == 2) {
                $("#search_team").hide();
                $("#search_members").show();
                $("#load_team").empty();
            }
        });

        $("#link_to_teamBuilder").append("Builder (ทีมช่าง)");

        //team builder manage
        $("#team_builder_search_btn").click(function() {
            blockPage();
            var teamCode = $("#team_code_search").val();
            var teamName = $("#team_name_search").val();
            var teamLead = $("#team_lead_search").val();
            var tSkill = $("#select2_2").val();
            var teamType = $("#team_type_search").val();
            var teamManager = $("#team_t_manager_search").val();
            if ($.trim(teamCode).length !== 0 ||
                    $.trim(teamName).length !== 0 ||
                    $.trim(teamLead).length !== 0 ||
                    tSkill !== null ||
                    $.trim(teamType).length !== 0 ||
                    $.trim(teamManager).length !== 0) {
//                alert("Search With Condition");
                $("#load_team").load("TeamMainPage.php?searchCondition=condition&teamCode=" + teamCode + "&teamName=" + teamName + "&teamLead=" + teamLead + "&teamSkill=" + tSkill + "&teamType=" + teamType + "&teamManager=" + teamManager, function() {
                    $.unblockUI();
                });
            } else {
                $("#load_team").load("TeamMainPage.php?searchCondition=search_all", function() {
                    $.unblockUI();
                });
            }
        });
        $("#team_builder_reset_btn").click(function() {
            blockPage();
            $("#load_member").empty();
            $("#test-load").load("TeamBuilderMainPage.php", function() {
                $.unblockUI();
            });
            setTimeout(function()
            {
                $("#search_members").hide();
                $("#search_team").show();
                $("#search_builder_change").val("1");
            }
            , 50);
        });

        //member builder manage
        $("#member_builder_search_btn").click(function() {
            blockPage();
            var member_id = $("#member_id").val();
            var member_name = $("#member_name").val();
            var role = $("#role").val();
            var team_code_in_member_form = $("#team_code_in_member_form").val();
            var t_name_in_search = $("#t_name_in_search").val();
            var select3_2 = $("#select3_2").val();
            if (!$.trim(member_id).length &&
                    !$.trim(member_name).length &&
                    !$.trim(role).length &&
                    !$.trim(team_code_in_member_form).length &&
                    !$.trim(t_name_in_search).length &&
                    !$.trim(select3_2)) {
                $("#load_member").load("MemberMainPage.php?searchCondition=search_all", function() {
                    $.unblockUI();
                });
            } else {
                $("#load_member").load("MemberMainPage.php?searchCondition=condition&memId=" + member_id + "&memName=" + member_name + "&memRole=" + role + "&memberSkill=" + select3_2 + "&teamCode=" + team_code_in_member_form + "&teamName=" + t_name_in_search, function() {
                    $.unblockUI();
                });
            }
        });
        $("#member_builder_reset_btn").click(function() {
            blockPage();
            $("#load_member").empty();
            $("#test-load").load("TeamBuilderMainPage.php", function() {
                $.unblockUI();
            });
            setTimeout(function()
            {
                $("#search_team").hide();
                $("#search_members").show();
                $("#search_builder_change").val("2");
            }
            , 50);

        });

    });
    function blockPage() {
        $.blockUI({message: 'Operation in progress...', css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .5,
                color: '#fff'
            }});
    }
</script>
<style type="text/css">
    .loader {
        position: fixed;
        left: 0px;
        top: 0px;
        width: 100%;
        height: 100%;
        z-index: 9999;
        background: url('image/page-loader.gif') 50% 50% no-repeat rgb(249,249,249);

    }
</style>